<template>
  <el-container class="layout-container">
    <!-- 侧边菜单 -->
    <el-aside :width="isCollapse ? '64px' : '200px'" class="sidebar">
      <div class="logo" @click="toggleCollapse">
        <div class="logo-content">
          <transition name="text-fade">
            <span v-show="!isCollapse" class="logo-text">Victory</span>
          </transition>
          <el-icon class="collapse-icon">
            <component :is="isCollapse ? Expand : Fold" />
          </el-icon>
        </div>
      </div>
      <el-menu
        :default-active="activeMenu"
        background-color="#304156"
        text-color="#bfcbd9"
        active-text-color="#409EFF"
        :collapse="isCollapse"
        :collapse-transition="false"
        :router="true"
        unique-opened
      >
        <el-menu-item index="/dashboard">
          <el-icon><PieChart /></el-icon>
          <template #title>控制台</template>
        </el-menu-item>

        <el-sub-menu index="1">
          <template #title>
            <el-icon><User /></el-icon>
            <span>用户管理</span>
          </template>
          <el-menu-item index="/users">用户列表</el-menu-item>
          <el-menu-item index="/roles">角色管理</el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="2">
          <template #title>
            <el-icon><Setting /></el-icon>
            <span>系统设置</span>
          </template>
          <el-menu-item index="/settings">基本设置</el-menu-item>
          <el-menu-item index="/logs">操作日志</el-menu-item>
        </el-sub-menu>
      </el-menu>
    </el-aside>

    <el-container class="main-container">
      <!-- 顶部导航 -->
      <el-header class="header">
        <div class="breadcrumb">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item v-for="item in breadcrumb" :key="item.path">
              {{ item.meta.title || item.name }}
            </el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <div class="header-right">
          <el-dropdown>
            <span class="user-info">
              <el-avatar :size="36" :src="authStore.user?.avatar" />
              <span class="username">{{ authStore.user?.name }}</span>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="goToProfile"
                  >个人中心</el-dropdown-item
                >
                <el-dropdown-item divided @click="handleLogout"
                  >退出登录</el-dropdown-item
                >
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <!-- 主内容区 -->
      <el-main class="content">
        <el-card class="main-card">
          <router-view v-slot="{ Component }">
            <transition name="fade" mode="out-in">
              <component :is="Component" />
            </transition>
          </router-view>
        </el-card>
      </el-main>

      <!-- 页脚 -->
      <el-footer class="footer">
        © 2023 管理系统 | Vue3 + Element Plus
      </el-footer>
    </el-container>
  </el-container>
</template>

<script setup>
import { computed, ref } from "vue";
import { useRouter, useRoute } from "vue-router";
import { useAuthStore } from "../stores/auth";
import { PieChart, User, Setting, Fold, Expand } from "@element-plus/icons-vue";

const authStore = useAuthStore();
const router = useRouter();
const route = useRoute();
const isCollapse = ref(false);

// 当前激活的菜单
const activeMenu = computed(() => route.path);

// 面包屑导航
const breadcrumb = computed(() => {
  const matched = route.matched.filter((item) => item.meta && item.meta.title);
  return matched;
});

// 切换菜单折叠状态
const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value;
};

// 退出登录
const handleLogout = () => {
  authStore.logout();
};

// 跳转到个人中心
const goToProfile = () => {
  router.push({ name: "Settings" });
};
</script>

<style scoped>
.layout-container {
  height: 100vh;
  display: flex;
}

.sidebar {
  background-color: #304156;
  height: 100%;
  transition: width 0.3s ease;
  overflow: hidden;
}

.main-container {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
}

.header {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid #e6e6e6;
  padding: 0 20px;
  background-color: #fff;
}

.content {
  background-color: #f0f2f5;
  padding: 20px;
  flex: 1;
  overflow: auto;
}

.main-card {
  height: 100%;
  box-sizing: border-box;
  border-radius: 6px;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 18px;
  font-weight: bold;
  background-color: #2b2f3a;
  cursor: pointer;
  transition: all 0.3s;
}

.logo:hover {
  background-color: #263445;
}

.logo-content {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  padding: 0 10px;
  box-sizing: border-box;
  white-space: nowrap;
}

.logo-text {
  display: inline-block;
  margin-right: 10px;
  transition: all 0.3s ease;
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 文字动画效果 */
.text-fade-enter-active,
.text-fade-leave-active {
  transition: max-width 0.3s ease, opacity 0.3s ease, margin-right 0.3s ease;
}

.text-fade-enter-from,
.text-fade-leave-to {
  max-width: 0;
  opacity: 0;
  margin-right: 0;
}

.collapse-icon {
  font-size: 18px;
  flex-shrink: 0;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background-color: #f5f7fa;
}

.username {
  margin-left: 10px;
  font-size: 14px;
}

.breadcrumb {
  font-size: 16px;
}

.footer {
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: #999;
  border-top: 1px solid #e6e6e6;
  background-color: #fff;
}

.el-menu {
  border-right: none;
}

.el-menu-vertical:not(.el-menu--collapse) {
  width: 200px;
}

/* 过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    z-index: 1000;
    height: 100%;
  }

  .main-container {
    margin-left: 0;
  }

  .is-collapse .main-container {
    margin-left: 64px;
  }
}
</style>